package com.bfxy.disruptor.common.exception;

import com.bfxy.disruptor.common.ConsumerRepository;
import com.bfxy.disruptor.common.event.EventHandler;
import com.bfxy.disruptor.common.consume.SingleConsumer;

public class ExceptionHandlerSetting<T> {
	private final EventHandler<T> eventHandler;
	private final ConsumerRepository<T> consumerRepository;

	public ExceptionHandlerSetting(final EventHandler<T> eventHandler, final ConsumerRepository<T> consumerRepository) {
		this.eventHandler = eventHandler;
		this.consumerRepository = consumerRepository;
	}

	public void with(ExceptionHandler<? super T> exceptionHandler) {
		((SingleConsumer<T>) consumerRepository.getEventProcessorFor(eventHandler))
				.setExceptionHandler(exceptionHandler);
		consumerRepository.getBarrierFor(eventHandler).alert();
	}
}
